package com.transcargo.rating;

import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.users.UserNotFoundException;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Date: 24.04.2011
 * Time: 16:13:59
 */
public class Rating {
    private int User_Id = 0;
    private Debt[] debts = null;
    private Feedback[] feedbacks = null;
    private Float rating = 0f;

    public Rating(int id, db dbase) throws UserNotFoundException {
        User_Id = id;
        try {
            ArrayList<String[]> list = dbase.execute("SELECT id FROM registered_users WHERE id=" + id);
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    String[] ctr = null;
                    list = dbase.execute("SELECT rating FROM ratings WHERE user_id=" + id);
                    iter = list.iterator();
                    if (iter.hasNext()) {
                        ctr = iter.next();
                        rating = Float.parseFloat(ctr[0]);
                    }
                    list = dbase.execute("SELECT id FROM feedbacks WHERE user_id=" + id);
                    iter = list.iterator();
                    feedbacks = new Feedback[list.size()];
                    int i = 0;
                    while (iter.hasNext()) {
                        ctr = iter.next();
                        feedbacks[i++] = new Feedback(Integer.parseInt(ctr[0]), dbase);
                    }
                    list = dbase.execute("SELECT id FROM debts WHERE debitor=" + id);
                    iter = list.iterator();
                    debts = new Debt[list.size()];
                    i = 0;
                    while (iter.hasNext()) {
                        ctr = iter.next();
                        debts[i++] = new Debt(Integer.parseInt(ctr[0]), dbase);
                    }
                } else throw new UserNotFoundException(id);
            } else throw new UserNotFoundException(id);
        } catch (dbException e) {
            throw new UserNotFoundException(id);
        }
    }

    public int getId() {
        return User_Id;
    }
    public Float getRating() {
        return rating;
    }
    public Debt[] getDebts() {
        return debts;
    }
    public Feedback[] getFeedbacks() {
        return feedbacks;
    }
    public static Float calculateRating(float in, int pos, int mark) {
        // TODO: Create valid rating calculator
        float res = new Float(in).floatValue();
        float Mark =new Float(mark).floatValue();
        if (pos == 0) return Mark; else {
            if (in >= 0 && mark > 0) {
                res = res + Mark * 0.5f;
            } else if (in <= 0 && mark < 0) {
                res = res + Mark * 0.5f;
            } else if (in < 0 && mark >= 0) {
                res = res + (Mark + 0.5f) * 0.25f;
            } else if (in > 0 && mark <= 0) {
                res = res + Mark * 0.25f;
            }
        }
        return res;
    }
}
